package com.example.benefitcommunity.service.impl;


import com.example.benefitcommunity.entity.BcCommunity;
import com.example.benefitcommunity.dao.BcCommunityDao;
import com.example.benefitcommunity.entity.Result;
import com.example.benefitcommunity.enums.StatusCode;
import com.example.benefitcommunity.service.BcCommunityService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 社区管理表(BcCommunity)表服务实现类
 *
 * @author makejava
 * @since 2025-08-06 16:53:17
 */
@Transactional(rollbackFor = Exception.class)
@Service("bcCommunityService")
public class BcCommunityServiceImpl implements BcCommunityService {
    @Resource
    private BcCommunityDao bcCommunityDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Result<Object> queryById(Integer id) {
        BcCommunity bcCommunity = this.bcCommunityDao.queryById(id);
        if (bcCommunity == null) {
            return new Result<>(StatusCode.ERROR.getCode(), StatusCode.ERROR.getMsg(), 0L, "查询失败！");
        }
        return new Result<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMsg(), 1L, bcCommunity);
    }

    /**
     * 分页查询
     *
     * @param bcCommunity 筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<BcCommunity> queryByPage(BcCommunity bcCommunity, PageRequest pageRequest) {
        long total = this.bcCommunityDao.count(bcCommunity);
        return new PageImpl<>(this.bcCommunityDao.queryAllByLimit(bcCommunity, pageRequest), pageRequest, total);
    }

    /**
     * 通过查询数据
     *
     * @param name     姓名
     * @param pagenum  页码
     * @param pagesize 页大小
     * @return 数据
     */
    @Override
    public Result<Object> queryByCommunity(String name, Integer pagenum, Integer pagesize) {
        PageHelper.startPage(pagenum, pagesize);
        BcCommunity bcCommunity = new BcCommunity();
        bcCommunity.setName(name);
        List<BcCommunity> list = this.bcCommunityDao.queryAll(bcCommunity);
        PageInfo<BcCommunity> pageInfo = new PageInfo<>(list);
        return new Result<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMsg(), pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * @param name 姓名
     * @return
     */
    @Override
    public Result<Object> queryByCommunityName(String name) {
        Map<String, Object> map = new HashMap<>();
        BcCommunity bcCommunity = new BcCommunity();
        bcCommunity.setName(name);
        List<BcCommunity> list = this.bcCommunityDao.queryAll(bcCommunity);
        map.put("Community", list);
        map.put("module", "社区管理");
        return new Result<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMsg(), (long) list.size(), map);
    }

    /**
     * 新增数据
     *
     * @param bcCommunity 实例对象
     * @return 实例对象
     */
    @Override
    public Result<Object> insert(BcCommunity bcCommunity) {
        List<BcCommunity> list = this.bcCommunityDao.queryAll(bcCommunity);
        if (list.isEmpty()) {
            if (this.bcCommunityDao.insert(bcCommunity) > 0) {
                return new Result<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMsg(), 1L, "社区新增成功！");
            }
            return new Result<>(StatusCode.ERROR.getCode(), StatusCode.ERROR.getMsg(), 0L, "社区新增失败！");
        }
        return new Result<>(StatusCode.ERROR.getCode(), StatusCode.ERROR.getMsg(), 0L, "社区已存在！");
    }

    /**
     * 修改数据
     *
     * @param bcCommunity 实例对象
     * @return 实例对象
     */
    @Override
    public Result<Object> update(BcCommunity bcCommunity) {
        BcCommunity bcCommunityNew = this.bcCommunityDao.queryById(bcCommunity.getId());
        if (bcCommunityNew != null) {
            if (this.bcCommunityDao.update(bcCommunity) > 0) {
                return new Result<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMsg(), 1L, "社区更新成功！");
            }
            return new Result<>(StatusCode.ERROR.getCode(), StatusCode.ERROR.getMsg(), 0L, "社区更新失败！");
        }
        return new Result<>(StatusCode.ERROR.getCode(), StatusCode.ERROR.getMsg(), 0L, "社区不存在！");
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public Result<Object> deleteById(Integer[] id) {
        int count = 0;
        //删除个别
        for (Integer i : id) {
            //删除单个
            if (this.bcCommunityDao.deleteById(i) > 0) {
                count++;
            }
        }
        //判断删除结果
        if (count == id.length) {
            return new Result<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMsg(), (long) id.length, "社区删除成功！");
        }
        return new Result<>(StatusCode.ERROR.getCode(), StatusCode.ERROR.getMsg(), 0L, "社区删除失败！");
    }
}
